// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
  const _ = db.command;
  var $ = db.command.aggregate
  return await db.collection('user_job').aggregate()
  .lookup({
    from: "users",
    localField: "userId",
    foreignField: "_openid",
    as: "userList"
  })
  .replaceRoot({
    newRoot: $.mergeObjects([ $.arrayElemAt(['$userList', 0]), '$$ROOT' ])
  })
  .project({
    userList: 0
  })
  .match({
    ...event.data
  }).end()
}